package com.example.thealarm;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity  {
    private FloatingActionButton floatingActionButton;
    private Toolbar toolbar;
    MyAdapter myAdapter;
    public static final int REQUEST_CODE = 1;
    static ArrayList<MusicFiles> musicFiles;
    static ArrayList<String> musicFilesNames;
    RecyclerView recyclerView;
    static ArrayList<String> Time,Date,Name,Type,Repeat,Repeat_NO,Repeat_TYPE,ID;
    ProgressDialog progressDialog;
    MyProvider provider;
    private NotificationHelper mNotificationHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.the_alarm_page);
        Init();
        permission();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            recreate();
        }
    }
        private void Init(){
        musicFilesNames = new ArrayList<>();
        mNotificationHelper = new NotificationHelper(this);
        recyclerView = findViewById(R.id.MYRecyclerView);
        provider=new MyProvider();
        toolbar=findViewById(R.id.TheToolbar);
        floatingActionButton =findViewById(R.id.TheFb);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            Intent intent = new Intent(view.getContext(),TheSetPage.class);
            startActivityForResult(intent, 1);
            //  SendOnChannel();
            }
        });
            Time=new ArrayList<>();
            Date=new ArrayList<>();
            Name=new ArrayList<>();
            Type=new ArrayList<>();
            Repeat = new ArrayList<>();
            Repeat_NO=new ArrayList<>();
            Repeat_TYPE = new ArrayList<>();
            ID=new ArrayList<>();
        displayData();
            myAdapter = new MyAdapter(this);
            recyclerView.setAdapter(myAdapter);
            DividerItemDecoration dI = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
            recyclerView.addItemDecoration(dI);
            recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        setSupportActionBar(toolbar);
        toolbar.setTitle("My alarm");
        }
    void displayData() {
        String a[] =new String[]{"1"};
        Cursor cr = getContentResolver().query(TheDataBase.BASE_CONTENT_URI,null,null,null,null);
        StringBuilder stringBuilder = new StringBuilder();
        if(cr==null){
            Toast.makeText(MainActivity.this,"Error",Toast.LENGTH_SHORT).show();
        }
        else
            while(cr.moveToNext()){
                String Id =cr.getString(0);
                String name =cr.getString(1);
                String date =cr.getString(2);
                String time =cr.getString(3);
                String repeat =cr.getString(4);
                String repeat_no =cr.getString(5);
                String repeat_type =cr.getString(6);
                ID.add(Id);
                Name.add(name);
                Date.add(date);
                Time.add(time);
                Repeat.add(repeat);
                Repeat_NO.add(repeat_no);
                Repeat_TYPE.add(repeat_type);
            }

        System.out.println("In Here?");
        Toast.makeText( MainActivity.this,stringBuilder.toString(),Toast.LENGTH_SHORT).show();
    }
    private void SendOnChannel(){
        System.out.println("In here");
        NotificationCompat.Builder nb = mNotificationHelper.getChannelNotification();
        mNotificationHelper.getManager().notify(1,nb.build());
    }
    private void permission(){

        if(ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)!=
                PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_CODE);
        }
        else {
            Toast.makeText(MainActivity.this,"Is Granted!",Toast.LENGTH_SHORT).show();
            musicFiles=getAudio(this);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == REQUEST_CODE){
            if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
                Toast.makeText(MainActivity.this,"Is Granted!",Toast.LENGTH_SHORT).show();
            }
            else {
                ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_CODE);
            }
        }
    }
    public static ArrayList<MusicFiles> getAudio(Context context){
        ArrayList<String> WhatHave = new ArrayList<>();
        ArrayList<MusicFiles> AudioList = new ArrayList<>();
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String[] projection={
                MediaStore.Audio.Media.ALBUM,
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DURATION,
                MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media._ID
        };
        Cursor cursor = context.getContentResolver().query(uri,projection,null,null,null,null);
        if(cursor!=null){
            while(cursor.moveToNext()){
                String album = cursor.getString(0);
                String title = cursor.getString(1);
                String duration = cursor.getString(2);
                String path = cursor.getString(3);
                String artist = cursor.getString(4);
                String id = cursor.getString(5);
                musicFilesNames.add(title);
                MusicFiles musicFiles = new MusicFiles(path,title,artist,album,duration,id);
                AudioList.add(musicFiles);
            }
            cursor.close();
        }
        return AudioList;
    }
    }

